package com.guoby.utils;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import java.text.DateFormat;
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;
//import java.util.Date;

/**
 * Created by v-jialiniu on 2018/3/8.
 */
public class DateUtil {
    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static String YYYY_MM_DD_FORMAT = "yyyy-MM-dd";
    public static String YYYYMMDD_FORMAT = "yyyyMMdd";
    public static String YYYY_MM_DD_HH_MM_SS_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String YYYYMMDDHHMMSS_FORMAT = "yyyyMMddHHmmss";

    /**
     * 获取系统当前日期
     *
     * @return Date
     */
    public static DateTime getTodayDate() {
        return new DateTime();
    }

    /**
     * 获取当前日期(时分秒为00)格式
     *
     * @return
     */
    public static DateTime getToday() {
        DateTime nowTime = new DateTime();
        DateTime startOfDay = nowTime.withTimeAtStartOfDay();
        return startOfDay;
    }

    /**
     * 获取系统当前日期
     *
     * @return
     */
    public static String getCurrentDate() {
        return getCurrentTime(YYYY_MM_DD_FORMAT);
    }

    /**
     * 获取系统当前时间
     *
     * @return
     */
    public static String getCurrentTime() {
        return getCurrentTime(YYYY_MM_DD_HH_MM_SS_FORMAT);
    }

    /**
     * 获取系统当前时间格式为传入参数格式
     *
     * @param format 时间格式
     * @return 返回format参数格式的时间格式字符串
     */
    public static String getCurrentTime(String format) {
        return dateToStr(getTodayDate(), format);
    }

    /**
     * date转为String
     */
    public static String dateToStr(DateTime date, String format) {
        if (null == date) {
            return null;
        }
        return date.toString(format);
    }

    /**
     * 将日期格式化,sourceFormat(如yyyy-MM-dd)格式的日期转为targetFormat(如yyyyMMdd)
     *
     * @param date
     * @return
     */
    public static String formatDate(String date, String sourceFormat, String targetFormat) {
        if (StringUtils.isBlank(date)) {
            return StringUtils.EMPTY;
        }
        return str2Date(date, sourceFormat).toString(targetFormat);
    }

    /**
     * string 转为 date
     */
    public static DateTime str2Date(String str) {
        return str2Date(str, YYYY_MM_DD_FORMAT);
    }

    /**
     * string 转为 date
     *
     * @param str
     * @param format
     * @return
     */
    public static DateTime str2Date(String str, String format) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
        DateTime dateTime = null;
        try {
            dateTime = formatter.parseDateTime(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateTime;
    }


}
